<?php

namespace app\common\model;

use think\Model;

/**
 * 数据层模型
 */
class Fupersonal extends Model
{

    /**
     * 查找
     */
    public function getList($condition = [], $field = '*', $page = 0, $order = 'ds_fuwu_personal.id desc')
    {
        if ($page) {
            $res = db('fuwu_personal')->where($condition)->field($field)
                ->join('ds_fuwu_skill s', 's.id = ds_fuwu_personal.skillid', 'left')
                ->join('ds_fuwu_grade g', 'g.id = ds_fuwu_personal.gradeid', 'left')
                ->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            return $res->items();
        } else {
            return db('fuwu_personal')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 查找
     */
    public function getListByTimeAndAddress($condition = [], $field = '*', $page = 0, $order = 'ds_fuwu_personal.id desc', $area = 0)
    {
        if ($page) {
            $reses = db('fuwu_personal')->where($condition)->field($field)
                ->join('ds_fuwu_skill s', 's.id = ds_fuwu_personal.skillid', 'left')
                ->join('ds_fuwu_grade g', 'g.id = ds_fuwu_personal.gradeid', 'left')
                ->order($order)->paginate($page, false, ['query' => request()->param()]);
            if ($area) {
                foreach ($reses as $k => $rese) {
                    $areaLists = db('fuwu_area')
                        ->where('fuwuid', $rese['id'])
                        ->where('status', 1)
                        ->select();
                    if (!$areaLists) {
                        unset($reses[$k]);
                        continue;
                    } else {
                        $areaIds = [];
                        foreach ($areaLists as $areaList) {
                            $areaIdArr = explode(',', $areaList['areaid']);
                            $areaIds = array_merge($areaIds, $areaIdArr);
                        }

                        if (!in_array($area, $areaIds)) {
                            unset($reses[$k]);
                            continue;
                        }
                    }
                }
            }

            $this->page_info = $reses;
            return $reses->items();
        } else {
            return db('fuwu_personal')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 添加
     */
    public function add($data)
    {
        return db('fuwu_personal')->insertGetId($data);
    }

    /**
     * 修改
     */
    public function edit($condition = [], $data)
    {
        return db('fuwu_personal')->where($condition)->update($data);
    }

    /**
     * 删除
     */
    public function del($condition = [])
    {
        return db('fuwu_personal')->where($condition)->delete();
    }

    /**
     * 获取单个详情
     */
    public function info($condition = [], $field = '*')
    {
        return db('fuwu_personal')->where($condition)->field($field)->find();
    }

    /**
     * 获取邀请人id和类型
     */
    public function getMember($code)
    {
        $returnArr = [];
        $res = $this->info(['code' => $code]);
        if ($res) {
            /**
             * 服务人员
             */
            $returnArr['prev'] = $res['id'];
            $returnArr['prevtype'] = 4;
        }
        $membermodel = model('member');
        $res2 = $membermodel->infoMember(['invite_code' => $code]);
        if ($res2) {
            /**
             * 运营商或者客户
             */
            $returnArr['prev'] = $res2['member_id'];
            if ($res2['member_type'] == 2) {
                $returnArr['prevtype'] = 2; #运营商
            }else if($res2['member_type'] == 3){  #运营商职员
                $returnArr['prevtype'] = 1; #运营商职员邀请 所属于平台
            } else {
                $returnArr['prevtype'] = 3; #客户
            }
        }
        if (empty($res) && empty($res2)) {
            $returnArr['prev'] = 0;
            $returnArr['prevtype'] = 1;
        }
        return $returnArr;
    }

    /**
     * 添加成功,修改积分
     */
    public function updateYue($previd, $prevtype)
    {
        $bool = true;
        if ($prevtype == 3) {
            $value = db('config')->where(['code' => 'purchase_goods'])->find();
            #普通客户邀请服务人员,修改普通用户积分
            $model = model('member');
            $money = $model->getMemberInfo(['member_id' => $previd], 'member_points');
            $vmoney = $money['member_points'] + intval($value['value']);
            $bool = $model->editMember(['member_id' => $previd], ['member_points' => $vmoney]);
        }
        if ($prevtype == 4) {
            #服务人员邀请服务人员,修改服务人员余额
            $value = db('config')->where(['code' => 'service_invitation'])->find();
            $money = $this->info(['id' => $previd], 'money');
            $vmoney = $money['money'] + intval($value['value']);
            $bool = $this->edit(['id' => $previd], ['money' => $vmoney]);
        }
        if ($bool) {
            return true;
        } else {
            return false;
        }
    }

    //生成运营商账号
    public function setService($phone, $password, $code, $fuwu_id, $prevtype, $prevcode)
    {
        #1、生成member表账号

        $member_type = 2; #用户账号类型
        $invite_type = 2; #邀请账号类型

        #服务人员不填写邀请码，归属到平台下面
        if (empty($prevcode)) {
            $member_type = 7; #用户账号类型
            $invite_type = 9; #邀请账号类型
        } else {
            if ($prevtype == 3) { #客户邀请服务人员   3--客户 2--运营商
                $member_type = 7;
                $invite_type = 9;
            }
        }
        $member_data = [
            'member_name' => $phone,
            'member_password' => md5($password),
            'member_avatar' => 'http://33wang.douyinhot.com/dd6c0202004231529237215.png',
            'member_addtime' => time(),
            'invite_parent_code' => $code,
            'supplier_type' => 2,
            'member_type' => $member_type,
            'is_service' => 1,
            'invite_code' => $this->createInviteCode(), // 添加邀请码
            'invite_type' => $invite_type
        ];
        $member_id = db('member')->insertGetId($member_data);
        // #2、生成店铺表 DB0EDB
        $store_data = [
            'store_name' => $phone,
            'member_id' => $member_id,
            'supplier_type' => 2,
            'store_seller_type' => 1,
            'storeclass_id' => 2,
            'store_addtime' => time(),
            'store_state' => 1,
        ];
        $store_id = db('store')->insertGetId($store_data);
        #3、生成入驻表
        $join_data = [
            'seller_name' => $phone,
            'member_id' => $member_id,
            'member_name' => $phone,
            'joinin_state' => 10,
            'store_seller_type' => 1,
            'supplier_type' => 2,
            'add_time' => time(),
            'joinin_state' => 40,
            'supplier_class' => 2
        ];
        db('storejoinin')->insertGetId($join_data);
         #4、生成运营商登录表
         $seller_data = [
             'seller_name' => $phone,
             'member_id' => $member_id,
             'store_id' => $store_id,
             'is_admin' => 1
         ];
         $seller_id = db('seller')->insertGetId($seller_data);
        return $member_id;
    }

    /**
     * @description: 生成唯一的邀请码  @MrLiu
     * @return string
     */
    public function createInviteCode()
    {
        $createCode = create_invite_code();

        return $createCode;
    }
}
